package org.bluedream.core.module.service.goods;

import com.aliyun.dysmsapi20170525.models.QueryShortUrlRequest;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.dao.goods.ProductDao;
import org.bluedream.core.module.entity.goods.Goods;
import org.bluedream.core.module.entity.goods.Product;
import org.bluedream.core.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @ClassName ProductService
 * @Description TODO
 * @Author Administrator
 * @Data 2022/7/4 8:58
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class ProductService extends OrgBaseServiceImpl<ProductDao, Product> implements IService<Product> {
    private static final Logger logger = LoggerFactory.getLogger(ProductService.class);

    @Override
    public Product getById(Serializable id) {
        if (EmptyUtil.isEmpty(id)){
            return new Product();
        }
        return super.getById(id);
    }

    public IPage<Product> findPage(Goods entity, String... args){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id" , entity.getId());
        queryWrapper.eq("code" , entity.getCode());
        queryWrapper.eq("org_code" , UserUtil.getLoginOrgCode());
        queryWrapper.orderByAsc("code" , "barcode");
        return super.findPage(new Product() , queryWrapper);
    }

    public RestResult getOne(Product entity){
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("id" , entity.getId());
        if (EmptyUtil.isNoEmpty(entity.getBarcode()) && EmptyUtil.isNoEmpty(entity.getCode())){
            qw.eq("barcode" , entity.getBarcode());
            qw.eq("code" , entity.getCode());
        }else {
            return RestResult.error("没有传入正确的商品条码或商品代码，请确认！");
        }
        List<Product> products = super.list(qw);
        if (products.size() == 1){
            return RestResult.ok();
        }
        return RestResult.error("返回了多条商品信息，请确认正确性");
    }

    public List<String> selectBarcode(QueryWrapper<Goods> queryWrapper){
        return baseMapper.selectBarcode(queryWrapper);
    }
}